package com.she.iiicoursecenter.view.tab1;

import ken.app.helper.Helper;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.she.iiicoursecenter.R;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_CourseReserve;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_CourseSet;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_CourseSingle;
import com.she.iiicoursecenter.activity.tab2_time.Tab2_CourseSelectedList;
import com.she.iiicoursecenter.bean.CourseSelectedListBundleBean;
import com.she.iiicoursecenter.bean.CourseSingleBundleBean;
import com.she.iiicoursecenter.bean.ViewChageDataBean;
import com.she.iiicoursecenter.event.base.Event;
import com.she.iiicoursecenter.event.connect.bean.UpdMakeBean;
import com.she.iiicoursecenter.event.connect.event.UpdMakeEvent;
import com.she.iiicoursecenter.event.iface.IEventCallback;
import com.she.iiicoursecenter.eventmanager.KenEventManager;
import com.she.iiicoursecenter.view.template.CustomizedView;

public class Tab1_CourseReserveView extends CustomizedView implements OnClickListener {
	//
	
	// test only!!!
	private ImageView	mImgBack , mImgClean, mImgupd_make;	
	private EditText	remark,	name, memberId,	email,	tel,	fax,	addr;
	private TextView 	contentid, itemName,contentName;
	private int contentId=0;
	private ProgressDialog dialog ;
	
	public Tab1_CourseReserveView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}
	
	private Handler handler = new Handler(){
		@Override
		public void dispatchMessage(Message msg) {
			// TODO Auto-generated method stub
			if (msg.what==0) {
				String[] title = { "確定"};
				Helper.ShowMessage((Activity) getContext(), "", "資料已送出\n請至預約記錄查詢進度", 1, title, null, null, null);
				super.dispatchMessage(msg);
				//記錄使用者資料
				KenEventManager.getInstance().setAddr(addr.getText().toString());
				KenEventManager.getInstance().setEmail(email.getText().toString());
				KenEventManager.getInstance().setFax(fax.getText().toString());
				KenEventManager.getInstance().setMemberId(memberId.getText().toString());
				KenEventManager.getInstance().setName(name.getText().toString());
				KenEventManager.getInstance().setRemark(remark.getText().toString());
				KenEventManager.getInstance().setTel(tel.getText().toString());
				// 單元預約成功後跳回
				if (getApp().tab2InCourseSingle) {
					// 搜尋單元預約成功後請跳至該系列的課程列表
					getApp().fadeout = true;
					Message message = Tab1_CourseReserveView.this.getApp().mChangeViewHandler.obtainMessage();
					message.what = Tab1_CourseReserveView.this.getApp().MSG_WHAT_CHANGEVIEW;
					CourseSelectedListBundleBean bundleBean = new CourseSelectedListBundleBean(getApp().tab2Date, getApp().tab2Slot, getApp().tab2Time);
					ViewChageDataBean dataBean = new ViewChageDataBean(Tab1_CourseReserve.class, Tab2_CourseSelectedList.class, bundleBean);
					message.obj = dataBean;
					Tab1_CourseReserveView.this.getApp().mChangeViewHandler.sendMessage(message);
				}else{
					// 一般單元預約成功後請跳至該系列的課程列表
					getApp().fadeout = true;
					getApp().getContentSingleBeanData().setResult(901);
					Message message = Tab1_CourseReserveView.this.getApp().mChangeViewHandler.obtainMessage();
					message.what 	= Tab1_CourseReserveView.this.getApp().MSG_WHAT_CHANGEVIEW;
					ViewChageDataBean   dataBean 	= new ViewChageDataBean(Tab1_CourseReserve.class, Tab1_CourseSet.class, null);
					message.obj							= dataBean;
					Tab1_CourseReserveView.this.getApp().mChangeViewHandler.sendMessage(message);
				}
				
			}else{
				Toast.makeText(getContext(), (String)msg.obj, Toast.LENGTH_SHORT).show();
			}
			dialog.cancel();
		}
	};

	private void init(){
		//
		LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.tab1_coursereserve, this, true);
		//
		dialog = new ProgressDialog(getContext());
		 dialog.setMessage("讀取中...");
		 dialog.setIndeterminate(true);
		 dialog.setCancelable(true);
		 
		//
		remark 	=	((EditText)Tab1_CourseReserveView.this.findViewById(R.id.tab1_reserve_remark));
		name	=	((EditText)Tab1_CourseReserveView.this.findViewById(R.id.tab1_reserve_name));
		memberId=	((EditText)Tab1_CourseReserveView.this.findViewById(R.id.tab1_reserve_memberid));
		email	=	((EditText)Tab1_CourseReserveView.this.findViewById(R.id.tab1_reserve_email));
		tel		=	((EditText)Tab1_CourseReserveView.this.findViewById(R.id.tab1_reserve_tel));
		fax		=	((EditText)Tab1_CourseReserveView.this.findViewById(R.id.tab1_reserve_fax));
		addr	=	((EditText)Tab1_CourseReserveView.this.findViewById(R.id.tab1_reserve_addr));
		
		remark.setText(KenEventManager.getInstance().getRemark());
		name.setText(KenEventManager.getInstance().getName());
		memberId.setText(KenEventManager.getInstance().getMemberId());
		email.setText(KenEventManager.getInstance().getEmail());
		tel.setText(KenEventManager.getInstance().getTel());
		fax.setText(KenEventManager.getInstance().getFax());
		addr.setText(KenEventManager.getInstance().getAddr());
		
		contentid	=	(TextView) Tab1_CourseReserveView.this.findViewById(R.id.tab1_reserve_contentid);
		itemName	=	(TextView) Tab1_CourseReserveView.this.findViewById(R.id.tab1_reserve_itemname);
		contentName	=	(TextView) Tab1_CourseReserveView.this.findViewById(R.id.tab1_reserve_contentname);
		//
		
		// test only!!!
		mImgBack = (ImageView)this.findViewById(R.id.img_back);
		mImgBack.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Message msg = Tab1_CourseReserveView.this.getApp().mChangeViewHandler.obtainMessage();
				msg.what 	= Tab1_CourseReserveView.this.getApp().MSG_WHAT_CHANGEVIEW;
				CourseSingleBundleBean bundleBean = new CourseSingleBundleBean( contentId, contentName.getText().toString());
				ViewChageDataBean   dataBean 	= new ViewChageDataBean(Tab1_CourseReserve.class, Tab1_CourseSingle.class, bundleBean);
				msg.obj							= dataBean;
				Tab1_CourseReserveView.this.getApp().mChangeViewHandler.sendMessage(msg);
				
			}});	
		
		// btn clean
		mImgClean = (ImageView) this.findViewById(R.id.tab1_reserve_btnclean);
		mImgClean.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//關閉鍵盤
//				Helper.keybordOff(getContext());
				remark.setText("");
				name.setText("");
				memberId.setText("");
				email.setText("");
				tel.setText("");
				fax.setText("");
				addr.setText("");
			}
		});
		
		//btn upd make
		mImgupd_make = (ImageView) this.findViewById(R.id.tab1_reserve_btn_upd_make);
		mImgupd_make.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				//關閉鍵盤
				Helper.keybordOff(getContext());
				
				if (name.getText().toString().trim().equals("") || email.getText().toString().trim().equals("") || tel.getText().toString().trim().equals("") || addr.getText().toString().trim().equals("") ) {
					String[] title = { "確定"};
					Helper.ShowMessage((Activity) getContext(), "", "請完成所有必填欄位", 1, title, null, null, null);
				}else{
					dialog.show();
					UpdMakeEvent updMakeEvent = new UpdMakeEvent( String.valueOf(contentId), name.getText().toString(), memberId.getText().toString(), 
							email.getText().toString(), tel.getText().toString(), fax.getText().toString(), addr.getText().toString(), remark.getText().toString());
					updMakeEvent.addCallback(new IEventCallback() {
						
						public void result(Event event, Object result) {
							// TODO Auto-generated method stub
							if (((UpdMakeBean)result).getResult() == 0) {
								handler.sendEmptyMessage(0);
							}else/* if (((UpdMakeBean)result).getResult() == 202)*/ {
								// 重複預約，請跳至該課程[資訊]頁
								Message msg = Tab1_CourseReserveView.this.getApp().mChangeViewHandler.obtainMessage();
								msg.what 	= Tab1_CourseReserveView.this.getApp().MSG_WHAT_CHANGEVIEW;
								CourseSingleBundleBean bundleBean = new CourseSingleBundleBean( contentId, contentName.getText().toString());
								ViewChageDataBean   dataBean 	= new ViewChageDataBean(Tab1_CourseReserve.class, Tab1_CourseSingle.class, bundleBean);
								msg.obj							= dataBean;
								Tab1_CourseReserveView.this.getApp().mChangeViewHandler.sendMessage(msg);
								Message message = handler.obtainMessage(1, ((UpdMakeBean)result).getMessage());
								handler.sendMessage(message);
							}
//							else{
//								Message msg = handler.obtainMessage(1, ((UpdMakeBean)result).getMessage());
//								handler.sendMessage(msg);
//							}
						}
						
						public void progress(Event event, int progress, String tmpPath) {
						}
					});
					KenEventManager.getInstance().execute(updMakeEvent);
				}
			}
		});
		
	}		
	
	public void setContentID(int contentid){
		this.contentId = contentid;
	}
	
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
